package cn.ynrcb.personnelfile.admin.dao;

import cn.ynrcb.personnelfile.admin.entity.NoticeSendEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 通知发送表
 */
@Mapper
public interface NoticeSendDao extends BaseMapper<NoticeSendEntity> {
    /**
     * 条条件查询分页列表
     */
    List<NoticeSendEntity> queryListPage(Page<NoticeSendEntity> page, @Param("map") Map<String, Object> params);

    /**
     * 条件查询列表
     */
    List<NoticeSendEntity> queryList(@Param("map") Map<String, Object> params);

    @Update("update tb_notice_send set is_read = 1, read_time = #{rendTime} where id= #{id} and user_id= #{userId} ")
    void setRead(Long id, Long userId, Date rendTime);

    /**
     * 设置已读
     * @param id
     */
    @Update("update tb_notice_send set is_read = 1,read_time = #{readTime},update_user_id = #{userId},update_time = #{readTime} where is_read = 0 and notice_id= #{id} ")
    void markRead(Long id,Long userId,Date readTime);

    @Select("SELECT count(1) from tb_notice_send where is_read= 0 and user_id= #{id}")
    Integer needReadNum(Long id);

    @Select("SELECT su.username as checked_tree_key, su.user_id from tb_notice_send tr inner join sys_user su on tr.user_id=su.user_id where tr.notice_id= #{id}")
    List<NoticeSendEntity> getCheckedTree(Integer id);

    @Delete("delete from tb_notice_send where notice_id = #{id}")
    void deleteByNoticeId(Long id);

    void deleteNoticeSend(@Param(value = "ids") List<Integer> ids);

    List<NoticeSendEntity> queryPage(Page<NoticeSendEntity> page,@Param(value = "map") Map<String, Object> params);

    @Update("update tb_notice_send set is_read=1 where is_read= 0 and user_id= #{currentUserId} ")
    void markAllReaded(Long currentUserId);
}
